package org.mspring.mlog.api.connect.qq.oauth;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.mspring.mlog.api.connect.qq.RandomStateGenerator;
import org.mspring.platform.utils.StringUtils;

/**
 * 
 * @author Gao Youbo
 * @since 2013年8月7日
 */
public class OAuth {

    /**
     * 获取AuthorizeURL
     * 
     * @return
     */
    public static String getAuthorizeURL() {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("response_type", "code"));
        params.add(new BasicNameValuePair("client_id", Config.getValue("APP_ID")));
        params.add(new BasicNameValuePair("redirect_uri", Config.getValue("redirectUrl")));
        params.add(new BasicNameValuePair("state", RandomStateGenerator.getUniqueState()));
        params.add(new BasicNameValuePair("scope", Config.getValue("scope")));
        return Config.getValue("authorizeUrl") + "?" + URLEncodedUtils.format(params, HTTP.UTF_8);
    }

    /**
     * 获取AccessTokenURl
     * 
     * @return
     */
    public static String getAccessTokenUrl(String code) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("grant_type", "authorization_code"));
        params.add(new BasicNameValuePair("client_id", Config.getValue("APP_ID")));
        params.add(new BasicNameValuePair("client_secret", Config.getValue("APP_KEY")));
        params.add(new BasicNameValuePair("code", code));
        params.add(new BasicNameValuePair("redirect_uri", Config.getValue("redirectUrl")));
        return Config.getValue("accessTokenUrl") + "?" + URLEncodedUtils.format(params, HTTP.UTF_8);
    }

    /**
     * 获取AccessToken
     * 
     * @param code
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static HttpResponse getAccessToken(String code) throws ClientProtocolException, IOException {
        String accessTokenUrl = getAccessTokenUrl(code);
        if (StringUtils.isBlank(accessTokenUrl)) {
            return null;
        }
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(accessTokenUrl);
        return httpClient.execute(httpGet);
    }

    public static Map<String, String> parseTokenString(String str) {
        HashMap<String, String> tokens = new HashMap<String, String>();
        str += "&";
        while (str.length() > 0) {
            String key_value = str.substring(0, str.indexOf("&"));
            String key = key_value.substring(0, key_value.indexOf("="));
            String value = key_value.substring(key_value.indexOf("=") + 1, key_value.length());
            tokens.put(key, value);
            str = str.substring(str.indexOf("&") + 1, str.length());
        }
        return tokens;
    }

    public static void main(String[] args) throws ClientProtocolException, IOException {
        String url = getAuthorizeURL();
        System.out.println(url);
    }
}
